<template>
  <div class="home">
    <!-- Logo -->
    <div class="logo">
      <img src="../assets/logo3-c3d3156c.jpg" alt="CoinETH">
    </div>

    <!-- Carousel -->
    <van-swipe :autoplay="3000" lazy-render>
      <van-swipe-item v-for="image in images" :key="image">
        <img :src="image" />
      </van-swipe-item>
    </van-swipe>

    <!-- Price Cards -->
    <div class="price-cards" v-if="coinList.length>0">
      <div class="price-card" v-for="(item,index) in coinList.slice(0,3)" :key="index">
        <div class="symbol">{{item.symbolDisplayName}}</div>
        <div class="price">{{item.tickers?.close?.toFixed(4) ?? ''}}</div>
        <div class="change" v-if="item.tickers" :class="{'up':(Number(divide(subtract(item.tickers.close,item.tickers.open),item.tickers.open))*100).toFixed(2)>0,'down':(Number(divide(subtract(item.tickers.close,item.tickers.open),item.tickers.open))*100).toFixed(2)<=0}">{{ (Number(divide(subtract(item.tickers.close,item.tickers.open),item.tickers.open))*100).toFixed(2) }}%</div>
        <div class="vertical-divider"></div>
      </div>
    </div>

    <!-- Function Navigation -->
    <div class="function-nav">
      <div class="nav-item">
        <div class="icon-circle">
          <img src="../assets/icon1.png" class="nav-icon" alt="recharge">
        </div>
        <span>{{ $t('home.recharge') }}</span>
      </div>
      <div class="nav-item" @click="goGetMoney">
        <div class="icon-circle">
          <img src="../assets/icon2.png" class="nav-icon" alt="withdraw">
        </div>
        <span>{{ $t('home.withdraw') }}</span>
      </div>
      <div class="nav-item">
        <div class="icon-circle">
          <img src="../assets/icon3.png" class="nav-icon" alt="service">
        </div>
        <span>{{ $t('home.service') }}</span>
      </div>
    </div>

    <!-- Coin List -->
    <div class="coin-list" v-if="coinList.length>0">
      <div class="coin-item" v-for="(item,index) in coinList" :key="index">
        <div class="coin-info">
          <img :src="item.symbolIcon" alt="BTC/USDT" class="coin-icon">
          <div class="coin-name">{{item.symbolDisplayName}}</div>
        </div>
        <div class="coin-price">{{item.tickers?.close?.toFixed(4) ?? ''}}</div>
        <div class="coin-change" v-if="item.tickers" :class="{'up':(Number(divide(subtract(item.tickers.close,item.tickers.open),item.tickers.open))*100).toFixed(2)>0,'down':(Number(divide(subtract(item.tickers.close,item.tickers.open),item.tickers.open))*100).toFixed(2)<=0}">{{ (Number(divide(subtract(item.tickers.close,item.tickers.open),item.tickers.open))*100).toFixed(2) }}%</div>
      </div>
      <!-- More coin items... -->
    </div>
    <div class="footer_body">
      <footer-nav></footer-nav>
    </div>
  </div>
</template>

<script setup name="Home">
import { ref } from "vue";
import img1 from '../assets/1-d10494b4.jpg';
import img2 from '../assets/2-a9ec783f.jpg';
import { HomeApi } from '@/api/home';
import {  subtract, divide } from '@/utils/calculator';
import { useI18n } from "vue-i18n";
const { t } = useI18n();
import { useRouter } from "vue-router";
const router = useRouter();
const images = [
  img1,
  img2
];

const goGetMoney = () => {
  router.push("/withdraw");
}
const coinList = ref([]);
const init = () => {
  HomeApi.getSymbolConfig().then(res => {
    console.log(res);
    if(res.code==200){
      coinList.value = res.data;
    }else{
      console.log(res.msg);
    }
  })
}
init()
</script>

<style scoped>
.home {
  height: 100vh;
  background: #121212;
  padding-bottom: 60px;
  overflow: auto;
}
.logo {
  padding: 2.5px 8px;
  margin-bottom: 12px;
  background: #121212;
  display: flex;
  justify-content: flex-start;
}
.logo img{
  width: 250px;
  height: 44px;
  object-fit: fill;
  filter: brightness(1.2);
}
.van-swipe{
  height: 200px;
  img{
    width: 85%;
    height: 100%;
    object-fit: fill;
  }
}
.carousel-container {
  position: relative;
  width: 100%;
  height: 200px;
  overflow: hidden;
}

.carousel-wrapper {
  display: flex;
  transition: transform 0.3s ease;
}

.carousel-slide {
  min-width: 100%;
}

.carousel-slide img {
  width: 100%;
  height: auto;
}

.carousel-indicators {
  display: flex;
  justify-content: center;
  padding: 8px 0;
}

.carousel-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: #ccc;
  margin: 0 4px;
}

.carousel-dot.active {
  background-color: #1989fa;
}

.price-cards {
  display: flex;
  justify-content: space-between;
  padding: 15px 8px;
  margin-bottom: 12px;
  background: #1e1e1e;
  border-radius: 8px;
  margin-top: 10px;
  font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif;
}

.price-card {
  flex: 1;
  text-align: center;
  position: relative;
  padding: 0 12px;
}

.price-card:not(:last-child){
  margin-right: 12px;
}
.price-card .symbol{
  font-size: 15px;
  color: #e0e0e0;
  margin-bottom: 6px;
  font-weight: 500;
}
.price-card .price{
  font-size: 18px;
  color: #ff5f57;
  margin-bottom: 6px;
  font-weight: 700;
  font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif;
}
.price-card .change.down{
  color: #ff5f57;
}
.price-card .change{
  font-size: 15px;
  font-weight: 500;
}
.price-card .change.up{
  color: #4ade80;
}
.vertical-divider {
  position: absolute;
  right: 0;
  top: 15%;
  height: 70%;
  width: 1px;
  background: rgba(255, 255, 255, .1);
}

.function-nav {
  display: flex;
  justify-content: space-between;
  height: 67px;
  padding: 0 40px;
  margin-bottom: 12px;
  background: #1e1e1e;
  border-radius: 8px;
}

.nav-item {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.nav-item span {
  font-size: 12px;
  color: #e0e0e0;
  font-weight: 400; 
}

.icon-circle {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: rgba(255, 255, 255, .1);
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 4px;
}

.nav-icon {
  width: 24px;
  height: 24px;
  margin-bottom: 4px;
  opacity: .9;
  filter: brightness(1.2);
}

.coin-list {
  margin-bottom: 65px;
  background: #1e1e1e;
  border-radius: 8px;
}

.coin-item {
  display: flex;
  align-items: center;
  padding: 20px 15px;
  position: relative;
  height: 68px;
  box-sizing: border-box;
}
.coin-item::after{
  content: "";
  position: absolute;
  left: 15px;
  right: 15px;
  bottom: 0;
  height: 1px;
  background: rgba(255, 255, 255, .1);
}

.coin-info {
  display: flex;
  align-items: center;
  flex: 1;
}

.coin-icon {
  width: 28px;
  height: 28px;
  margin-right: 12px;
  filter: brightness(1.2);
}
.coin-name{
  font-size: 15px;
  color: #e0e0e0;
  font-weight: 500;
}
.coin-price{
  font-size: 15px;
  color: #e0e0e0;
  margin-right: 15px;
  text-align: right;
  flex: 1;
  font-weight: 700;
}
.coin-change{
  min-width: 60px;
  text-align: center;
  padding: 4px 8px;
  font-size: 12px;
  color: #fff;
  border-radius: 4px;
}
.coin-change.up{
  background: #4ade80;
}
.coin-change.down{
  background: #ff5f57;
}
.van-swipe-item{
  text-align: center;
}
</style>